﻿package com.powerunion.extjs.utils;

import java.util.Calendar;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class DateTimeUtil {
    /**
     * 由java.util.Date到java.sql.Date的类型转换
     *
     * @param date
     * @return Date
     */
    public static Date getSqlDate(java.util.Date date) {
        return new Date(date.getTime());
    }

    public static Date nowDate() {
        Calendar calendar = Calendar.getInstance();
        return getSqlDate(calendar.getTime());
    }

    public static Date nowTime() {
        Calendar calendar = Calendar.getInstance();
        return getSqlDate(calendar.getTime());
    }
    /**
     * 获得某一日期的后一天字符串,
     *
     * @return Date
     */
    public static String getNextDateString(int year, int month, int day) {
        String result = null;
        Date date = buildDate(year, month, day);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day1 = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day1 + 1);
        date = getSqlDate(calendar.getTime());
        result = getDateString(date, "yyyyMMdd");
        return result;
    }

    /**
     * 获得某一日期的后一天
     *
     * @param date
     * @return Date
     */
    public static Date getNextDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day + 1);
        return getSqlDate(calendar.getTime());
    }

    /**
     * 获得某一日期的前2天
     *
     * @param date
     * @return Date
     */
    public static Date getPreviousDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day - 2);
        return getSqlDate(calendar.getTime());
    }

    /**
     * 获得某年某月第一天的日期
     *
     * @param year
     * @param month
     * @return Date
     */
    public static Date getFirstDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DATE, 1);
        return getSqlDate(calendar.getTime());
    }

    /**
     * 获得某年某月最后一天的日期
     *
     * @param year
     * @param month
     * @return Date
     */
    public static Date getLastDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, 1);
        return getPreviousDate(getSqlDate(calendar.getTime()));
    }

    /**
     * 由年月日构建java.sql.Date类型
     *
     * @param year
     * @param month
     * @param date
     * @return Date
     */
    public static Date buildDate(int year, int month, int date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date);
        return getSqlDate(calendar.getTime());
    }

    /**
     * 取得某月的天数
     *
     * @param year
     * @param month
     * @return int
     */
    public static int getDayCountOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, 0);
        return calendar.get(Calendar.DATE);
    }

    /**
     * 获得某年某季度的最后一天的日期
     *
     * @param year
     * @param quarter
     * @return Date
     */
    public static Date getLastDayOfQuarter(int year, int quarter) {
        int month = 0;
        if (quarter > 4) {
            return null;
        } else {
            month = quarter * 3;
        }
        return getLastDayOfMonth(year, month);

    }

    /**
     * 获得某年某季度的第一天的日期
     *
     * @param year
     * @param quarter
     * @return Date
     */
    public static Date getFirstDayOfQuarter(int year, int quarter) {
        int month = 0;
        if (quarter > 4) {
            return null;
        } else {
            month = (quarter - 1) * 3 + 1;
        }
        return getFirstDayOfMonth(year, month);
    }

    /**
     * 获得某年的第一天的日期
     *
     * @param year
     * @return Date
     */
    public static Date getFirstDayOfYear(int year) {
        return getFirstDayOfMonth(year, 1);
    }

    /**
     * 获得某年的最后一天的日期
     *
     * @param year
     * @return Date
     */
    public static Date getLastDayOfYear(int year) {
        return getLastDayOfMonth(year, 12);
    }

    /**
     * String到java.sql.Date的类型转换
     *
     * @param param
     * @return Date
     */
    public static Date StringToDate(String param) {
        if(param == null || param == "" || param.indexOf("null") >=0) {
            return null;
        }
        else {
            java.util.Date date = null;
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                param = param.replaceAll("年", "-").replaceAll("月", "-").replaceAll("日", "-");
                date = sdf.parse(param);
                return new Date(date.getTime());
            } catch (ParseException ex) {
                //ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 得到日期字符串
     *
     * @param date
     * @param partten
     * @return
     */
    public static String getDateString(Date date, String partten) {
    	if(date == null) date = DateTimeUtil.nowDate();
        String result = null;
        String timezoneID = "GMT+8:00";
        SimpleDateFormat formatter = new SimpleDateFormat(partten, new java.util.Locale("zh", "CN"));
        java.util.TimeZone timezone = java.util.TimeZone.getTimeZone((String) timezoneID);
        Calendar calendar = new java.util.GregorianCalendar(timezone);
        formatter.setCalendar(calendar);
        result = formatter.format(date);
        return result;
    }

    public static void main(String[] argv) {
        System.out.println(DateTimeUtil.getDateString(null, "yyyy-MM-dd hh:mm:ss"));

    }
}